<!-- 查询商品优惠券信息 -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row :gutter="48">
                    <a-col :md="8" :sm="24">
                        <a-form-item label="手机号">
                            <a-input v-model="phone" allow-clear placeholder="请输入手机号" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="用户ID">
                            <a-input v-model="userId" allow-clear placeholder="请输入用户ID" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="优惠券名称">
                            <a-input v-model="name" allow-clear placeholder="请输入优惠券名称" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="优惠券来源">
                            <a-select v-model="type" placeholder="请选择" allowClear @change="getData()">
                                <a-select-option v-for="(item,i) in typeList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="优惠券状态">
                            <a-select v-model="status" placeholder="请选择" allowClear @change="getData()">
                                <a-select-option v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="12" :sm="24">
                        <a-form-item label="日期">
                            <a-range-picker v-model="dateValue" @change="dateChange"/>
                        </a-form-item>
                    </a-col>
                    <a-col :md="4" :sm="24">
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item >
                            <a-button style="margin-right: 20px" type="primary" @click="getData()">查询</a-button>
                            <a-button icon="plus" @click="add()">手动发放</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="false" size="small">
            <div slot="type" slot-scope="type">
                <div v-for="(item,i) in typeList" :key="i">
                    <a-tag v-if="item.id == type" :color="colors[i%13]">{{item.name}}</a-tag>
                </div>
            </div>
            <div slot="amount" slot-scope="amount">
                <div>{{amount}}元</div>
            </div>
            <div slot="requirement" slot-scope="requirement">
                <div>{{requirement}}元</div>
            </div>
            <div slot="status" slot-scope="status">
                <div v-for="(item,i) in statusList" :key="i">
                    <a-tag v-if="item.id == status" :color="colors[i%13]">{{item.name}}</a-tag>
                </div>
            </div>
            <template slot="operation" slot-scope="text, record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="update(record)">修改</a-button>
                    <a-popconfirm
                        title="是否确认删除当前优惠券信息?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="del(record)"
                    >
                        <a-button type="link" class="errorColor" >删除</a-button>
                    </a-popconfirm>
                </div>
            </template>
        </a-table>

        <!-- 手动发放 -->
        <a-modal
            title="手动发放优惠券"
            :visible="visible"
            :confirm-loading="confirmLoading"
            @ok="handleOk"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="用户ID">
                        <a-input v-model="item.userId" placeholder="请输入用户ID" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="优惠券名称">
                        <a-input v-model="item.name" placeholder="请输入优惠券名称" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="优惠券金额">
                        <a-input v-model="item.amount" placeholder="请输入优惠券金额" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="优惠券最低使用金额">
                        <a-input v-model="item.requirement" placeholder="请输入优惠券最低使用金额" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="优惠券类型">
                        <a-radio-group v-model="item.type" >
                            <a-radio :value="type.id" v-for="type in typeList" :key="type.id">{{type.name}}</a-radio>
                        </a-radio-group>
                    </a-form-item>
                    <a-form-item label="有效期（天）" >
                        <a-input v-model="item.validTime" placeholder="请输入有效期（天）" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="备注">
                        <a-input v-model="item.remarks" placeholder="请输入备注" allow-clear></a-input>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>


</template>

<script>
import { columns } from './table'
import { queryGoddsCouponInfo , manualExtendGoodsCoupon } from '@/api/product'
export default {
    data () {
        return {
            brandId: this.global.brandId,
            brandList: [],
            page: 1,
            size: 10,
            total: 0,
            dataList: [],
            columns,
            isload: false,
            phone: '',
            userId: '',
            name: '',
            startTime: '',
            endTime: '',
            dateValue: [],
            type: undefined,
            typeList: [
                { id: 1, name: '平台福利'},
                { id: 2, name: '理赔'},
                { id: 0, name: '注册'},
            ],
            status: undefined,
            statusList: [
                { id: 0, name: '未使用'},
                { id: 1, name: '已使用'},
                { id: 2, name: '已过期'},
            ],
            isload: false,
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
            visible: false,
            confirmLoading: false,
            item: {
                id: '',
                userId: '',
                name: '',
                type: '1',
                amount: '',
                requirement: '', // 使用条件
                validTime: '', // 有效期（天）
            },
        }
    },
    async created(){
        try {
            this.getData()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: '网路丢失，请稍后再试！',
                duration: 8,
            })
        }
    },
    methods: {
        // 查询产品详情信息配置
        getData(){
            var _this = this
            _this.isload = true
            const params = {
                brandId: _this.brandId,
                phone:_this.phone,
                userId: _this.userId,
                name: _this.name,
                type: _this.type,
                startTime: _this.startTime,
                endTime: _this.endTime,
                status:_this.status,
                page: _this.page - 1,
                size: _this.size,
            }
            queryGoddsCouponInfo(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                    console.log(_this.dataList,'dataList')
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        dateChange(date,dateString){
            this.startTime = dateString[0];
            this.endTime = dateString[1];
            this.getData();
        },
        // 添加优惠券
        add(){
            this.item.id = ''
            this.item.userId = ''
            this.item.name =  ''
            this.item.type = 1
            this.item.amount = ''
            this.item.requirement = ''
            this.item.validTime = ''
            this.item.remarks = ''
            this.visible = true;
        },
        // 修改
        update(item){
            this.item = JSON.parse(JSON.stringify(item))
            this.visible = true
        },
        // 确认发放
        handleOk(){
            let _this = this;
            if(_this.item.userId == ''){
                _this.$message.warning('请输入用户ID');
                return
            }
            if(_this.item.name == ''){
                _this.$message.warning('请输入优惠券名称');
                return
            }
            if(_this.item.amount == ''){
                _this.$message.warning('请输入优惠券金额');
                return
            }
            if(_this.item.requirement == ''){
                _this.$message.warning('请输入优惠券使用条件');
                return
            }
            if(_this.item.validTime == ''){
                _this.$message.warning('请输入优惠券使用天数');
                return
            }
            const params = {
                'id': _this.item.id,
                'userId': _this.item.userId,
                'name': _this.item.name,
                'amount': _this.item.amount,
                'requirement': _this.item.requirement,
                'validTime': _this.item.validTime,
                'remarks': _this.item.remarks,
                'type': _this.item.type,
            }
            _this.confirmLoading = true;
            manualExtendGoodsCoupon(params)
            .then(res => {
                _this.confirmLoading = false;
                if(res.success){
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.item.id == '' ? '添加成功' : '修改成功',
                        duration: 8,
                    })
                    _this.visible = false;
                    _this.getData();
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除
        del(item){
            let _this = this
            const params = {
                'brandId':item.brandId,
                'id':item.id,
                'userId': item.userId,
                'name': item.name,
                'amount': item.amount,
                'requirement': item.requirement,
                'validTime': '30',
                'remarks': item.remarks,
                'isDelete':1,
            }
            manualExtendGoodsCoupon(params)
            .then(res=>{
                if(res.success){
                    _this.getData()
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>
.imgUrl{
    width: 180px;
    height: 100px;
}
.ant-carousel >>> .slick-slide img {
    text-align: center;
    height: 300px;
    line-height: 300px;
    background: #364d79;
    overflow: hidden;
    width: 100%;
}
.ant-carousel >>> .slick-thumb {
  bottom: -15px;
}
.ant-carousel >>> .slick-thumb li {
  width: 60px;
  height: 45px;
}
.ant-carousel >>> .slick-thumb li img {
  width: 100%;
  height: 100%;
  filter: grayscale(100%);
}
.ant-carousel >>> .slick-thumb li.slick-active img {
  filter: grayscale(0%);
}
</style>